import stockdata
import shelve
import csv
from datetime import date

START_DATE = date(2011,10,15)#'2011-10-15'
END_DATE = date(2013,10,15)#'2013-10-15'
FILE_NAME = 'companylist.csv'
DB_NAME = 'stock_data.she'

with open(FILE_NAME) as csvfile:
    stocks = [row[0] for row in csv.reader(csvfile)][1:]

db = shelve.open(DB_NAME, 'c')

counter = 1
for stock in stocks:
    print ''
    print 'Stock %d out of %d (%s)' % (counter, len(stocks), stock)
    if stock not in db:
        db[stock] = stockdata.get_stock_data(stock, START_DATE, END_DATE)
    else:
        print '%s is already in the database!' % stock
    counter += 1

db.close()